package com.coffeeloversupportlibrary.coffeelover.domain.coffeebean.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.RecyclerView;


import com.coffeeloversupportlibrary.R;
import com.coffeeloversupportlibrary.coffeelover.domain.coffeebean.type.ContentType;
import com.coffeeloversupportlibrary.databinding.BeansContentBinding;


import java.util.ArrayList;
import java.util.List;

/**
 * @author puyv
 * @createTime 2021/11/12 11:53
 * @projectName CoffeeLoverSupportLibrary
 * @className BeanMainAdapter.java
 * @description TODO
 */
public class BeanMainAdapter extends RecyclerView.Adapter {
    private List<ContentType> data;
    private FragmentManager fragmentManager;

    public BeanMainAdapter(List<ContentType> list){
        this.data=list;
    }

    public void rebuild(FragmentManager fragmentManager){
          this.fragmentManager=fragmentManager;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.beans_content,parent,false);
            BeansContentBinding beansContentBinding= DataBindingUtil.bind(view);
            return new ContentViewHolder(beansContentBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
              ContentType item=(ContentType) data.get(position);
              List<String> titles=item.getTitles();
              List< Fragment > lists=new ArrayList<>();


              for (int i = 0; i < titles.size(); i++) {
                  ((ContentViewHolder)holder).beansContentBinding.beanTabTitle
                          .addTab( ((ContentViewHolder)holder).beansContentBinding.beanTabTitle.newTab().setText(titles.get(i)));

                  BeansListFragment fragment =new BeansListFragment();
                  lists.add(fragment);
              }

              BeansFragmentAdapter adapter=new BeansFragmentAdapter(fragmentManager,lists,titles);

              ((ContentViewHolder)holder).beansContentBinding.beanPage.setAdapter(adapter);

              ((ContentViewHolder)holder).beansContentBinding.beanTabTitle.setupWithViewPager(  ((ContentViewHolder)holder).beansContentBinding.beanPage);

    }




    @Override
    public int getItemCount() {
        return data.size();
    }

    static class ContentViewHolder extends RecyclerView.ViewHolder{
        BeansContentBinding beansContentBinding;
        public ContentViewHolder(@NonNull BeansContentBinding beansContentBinding) {
            super(beansContentBinding.getRoot());
            this.beansContentBinding=beansContentBinding;
        }
    }

}
